package com.twxm.breed.iot;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class TimeWheel {

    ArrayList<TimerTask> tasks = new ArrayList<>();

    public TimeWheel() {
    }


    public void addTask(TimerTask task){
        int index = tasks.size();
        tasks.add(task);
    }

    public void removeTask(String key){
        // 移除任务
        for(TimerTask task:tasks){
            if(task.getFishPondId().equals(key)){
                tasks.remove(task);
                return;
            }

        }
    }

    public boolean runTasks(ArrayList<TimerTask> onceTasks, ArrayList<TimerTask> multiTasks){
        if(tasks.size() == 0){
            return false;
        }
        for (TimerTask task:
             tasks) {
            task.run();
            if(task.getPeriod() != 0){
                multiTasks.add(task);
            }else{
                onceTasks.add(task);
            }
        }
        return true;
    }
}
